package org.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.SecurityUtils;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pojo.*;
import org.dk.service.AddressService;
import org.dk.service.CityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("addressController")
@Controller
public class AddressController {

    @Reference
    private AddressService addressService;

    @Reference
    private CityService cityService;

    @RequestMapping("toHome")
    public String toHome(){
            return "home-index";
        }

    @RequestMapping("toAddress")
    public String toAddress(){
            return "home-setting-address";
        }

     @RequestMapping("toOrderSend")
    public String toOrderSend(){
        return "home_order_send";
    }

    @RequestMapping("selectAddress")
    @ResponseBody
    public DataResult selectAddress(){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            List<TbAddress> addresses = addressService.selectAddress(user.getUsername());

            List<TbProvinces> provinces = cityService.selectProvinces();

            List<TbCities> cities = cityService.selectCities();

            List<TbAreas> areas = cityService.selectAreas();

            Map<String,Object> map = new HashMap<>();
            map.put("addresses",addresses);
            map.put("provinces",provinces);
            map.put("cities",cities);
            map.put("areas",areas);

            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(map);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }

    @RequestMapping("selectCity")
    @ResponseBody
    public DataResult selectCity(Integer obj,String id){
        try {
            if(obj == 0){
                List<TbProvinces> provinces = cityService.selectProvinces();
                return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(provinces);
            }else if (obj == 1){
                List<TbCities> cities = cityService.selectCitiesById(id);
                return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(cities);
            }else if(obj == 2){
                List<TbAreas> areas = cityService.selectAreasById(id);
                return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(areas);
            }else {
                return DataResult.buildDataResult(DataResultEnum.ERROR);
            }
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }

    @RequestMapping("addAddress")
    @ResponseBody
    public DataResult addAddress(TbAddress address) {
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getUsername());
            address.setIsDefault("0");
            address.setCreateDate(new Date());
            addressService.addAddress(address);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteAddress")
    @ResponseBody
    public DataResult deleteAddress(Long id){
        try {
            addressService.deleteAddress(id);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }
    @RequestMapping("updateAddress")
    @ResponseBody
    public DataResult updateAddress(Long id){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            addressService.updateAddress(id,user.getUsername());
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }

    }
}
